/**
 * Created by root on 19-10-20.
 * w分量的影响
 */

#ifndef OPENGLESPRO_GAME_STEP_4_H
#define OPENGLESPRO_GAME_STEP_4_H

#define VERTEX_ATTR_INDEX_V4_POSITION 0
#define VERTEX_ATTR_INDEX_V4_COLOR 1
#define VERTEX_ATTR_INDEX_V4_MATRIX 2
#define VERTEX_VEC_COUNT_V4_PER_POSITION 4
#define VERTEX_VEC_COUNT_V4_PER_COLOR 3
#define STRIDE_BYTES_V4 sizeof(GLfloat)*(VERTEX_VEC_COUNT_V4_PER_POSITION+VERTEX_VEC_COUNT_V4_PER_COLOR)

void OnSurfaceCreatedV4();

void OnSurfaceCreatedV4() {}

void OnSurfaceChangedV4();

void OnSurfaceChangedV4() {}

void OnDrawFrameV4();

void OnDrawFrameV4() {
    GLfloat vertexPointers[] = {
            // Order of coordinates: X, Y, Z, W, R, G, B, A

            // Triangle Fan
            0, 0, 0, 1.5, 1, 1, 1,
            -0.5, -0.8, 0, 1, 0.7, 0.7, 0.7,
            0.5, -0.8, 0, 1, 0.7, 0.7, 0.7,
            0.5, 0.8, 0, 2, 0.7, 0.7, 0.7,
            -0.5, 0.8, 0, 2, 0.7, 0.7, 0.7,
            -0.5, -0.8, 0, 1, 0.7, 0.7, 0.7,

            // Line
            -0.5, 0, 0, 1.5, 1, 0, 0,
            0.5, 0, 0, 1.5, 1, 0, 0,

            // Mallets
            0, -0.4, 0, 1.25, 0, 0, 1,
            0, 0.4, 0, 1.75, 1, 0, 0
    };
    glEnableVertexAttribArray(VERTEX_ATTR_INDEX_V4_COLOR);
    glEnableVertexAttribArray(VERTEX_ATTR_INDEX_V4_POSITION);
    glLineWidth(3);
    glVertexAttribPointer(VERTEX_ATTR_INDEX_V4_POSITION, VERTEX_VEC_COUNT_V4_PER_POSITION, GL_FLOAT,
                          GL_FALSE, STRIDE_BYTES_V4, vertexPointers);
    glVertexAttribPointer(VERTEX_ATTR_INDEX_V4_COLOR, VERTEX_VEC_COUNT_V4_PER_COLOR, GL_FLOAT,
                          GL_FALSE, STRIDE_BYTES_V4,
                          &vertexPointers[VERTEX_VEC_COUNT_V4_PER_POSITION]);
    int w = device.width;
    int h = device.height;
    if (w > h) {
        GLfloat landMatrix[] = {
                (float) h / (float) w, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                0, 0, 0, 1
        };
        glUniformMatrix4fv(VERTEX_ATTR_INDEX_V4_MATRIX, 1, GL_FALSE, landMatrix);
    } else {
        GLfloat portraitMatrix[] = {
                1, 0, 0, 0,
                0, (float) w / (float) h, 0, 0,
                0, 0, 1, 0,
                0, 0, 0, 1
        };
        glUniformMatrix4fv(VERTEX_ATTR_INDEX_V4_MATRIX, 1, GL_FALSE, portraitMatrix);
    }
    glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
    glDrawArrays(GL_LINES, 6, 2);
    glDrawArrays(GL_POINTS, 8, 1);
    glDrawArrays(GL_POINTS, 9, 1);
}

#endif //OPENGLESPRO_GAME_STEP_4_H
